import axios from "axios";
import { Message } from "element-ui";
import router from '../router/index'
// import { message } from "element-ui";

const request = axios.create({
    baseURL:'https://api.shop.eduwork.cn/',
    timeout:5000
})


//  请求拦截
request.interceptors.request.use((config)=>{
    const token = window.localStorage.getItem('token')
    if(token){
        config.headers.Authorization = 'Bearer '+ token
    }
    return config
})
//
// //  相应拦截
//
request.interceptors.response.use((res)=>{
    return res
    
},error =>{
    if(!error.response){
        Message.error('网络错误，请稍后再试！')
        return
      }
   const status = error.response.status
   switch(status){
       case 400:
           Message.error(error.response.data.message)
           break;
        case 401:
           Message.error('登录失效，请重新登录')
           router.push({path:'/login'})
           break;
        default:
            return error.response
   }
 })

export default  request